/*!
// Testweb v0.4.0
// http://or-change.cn
// Copyright 2014 OrChange Inc. All rights reserved.
// Licensed under the GPL License.
*/
define(function (require, exports, module) {
	'use strict';
	var view = require("lib/core/controller"),
		$ = require("lib/jquery/jquery");

	require("lib/jquery/jquery.ui");
	exports.moveToCenter = function (template_name, focus) {
		var c_height, c_width, w_height, w_width, body = $("body").get(0),
			tpl = $("[tpl=" + template_name + "]");

		if (typeof focus !== "boolean") {
			focus = false;
		}
		c_height = tpl.get(0).clientHeight;
		c_width = tpl.get(0).clientWidth;
		w_height = body.clientHeight;
		w_width = body.clientWidth;

		if (!tpl.attr("style").match(/;?top:|;?left:/) || focus) {
			tpl.css({
				"top": (w_height - c_height) / 2 + "px",
				"left": (w_width - c_width) / 2 + "px"
			});
		}
	};

	exports.initWindowTemplate = function (template_name) {
		var window_template = $("[tpl=" + template_name + "]");
		$(".bar-close", window_template.get(0)).click(function () {
			view.close(template_name);
		});

		window_template.draggable({
			handle: ".title",
			start: function () {
				$("#mask").addClass("transparent").css("display", "block");
			},
			stop: function () {
				var data = {};
				$("#mask").removeClass("transparent").css("display", "none");
				data.position_top = $(this).css("top");
				data.position_left = $(this).css("left");
				view.getTemplate(template_name).setData(data);
			}
		});

		window_template.bind("click mousedown mouseup", function () {
			$("[tpl]").css("z-index", "1");
			$(this).css("z-index", "10");
		});
	};

	exports.component = function (context) {
		$.component(context);
	};
});